var express = require('express')
var app = express()
var bodyParser = require('body-parser')
var fs = require('fs')
//设置跨域访问
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

// 创建 application/x-www-form-urlencoded 编码解析
var urlencodedParser = bodyParser.urlencoded({extended: false})


var questions = [
    {
        data: 213,
        num: 444,
        age: 12
    },
    {
        data: 456,
        num: 678,
        age: 13
    }];

//写个接口123
app.get('/user', function (req, res) {
    console.log(req.query)
    if (req.query) {
        res.status(200),
            res.json(questions)
    } else {
        res.status(200),
            res.json({msg: '用户名或密码错误'})
    }
});

app.post('/login', urlencodedParser, function (req, res) {
    console.log(req.body)
    res.status(200)
    if (req.body.name == 'xwy' && req.body.password == '123456') {

        res.json({code: 200, data: questions, msg: '登录成功'})
    } else {
        res.json({code: '300', msg: '用户名或密码错误'})
    }
})
//配置服务端口
var server = app.listen(8000, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log('listen', host, port);
})
